// pages/myPan/settings/editAddress/editAddress.js
var util = require('../../../../utils/util.js');
import api from '../../../../api/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 0,
    sending: false,
    duration: 120,
    time: "",
    phone: "",
    sms: "",
    pwd: "",
    rePwd: "",
    v_text: "获取验证码",
    userInfo: "",
    loginType: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 1) {
      wx.setNavigationBarTitle({
        title: '忘记密码',
      })
    }
    var self = this
    wx.showLoading({
      mask:true
    })
    api.getUser({
      done: (user) => {
        wx.hideLoading()
        self.data.phone = user.phone || user.vendor_phone
        self.setData({
          userInfo:user
        })
      }, fail: (user) => {
        wx.hideLoading()
      }
    })
  },
  onInput: function (e) {
    switch (e.target.dataset.name) {
      case "phone":
        this.setData({
          phone: util.trim(e.detail.value)
        });
        break;
      case "sms":
        this.setData({
          sms: util.trim(e.detail.value)
        });
        break;
      case "pwd":
        this.setData({
          pwd: util.trim(e.detail.value)
        });
        break;
      case "rePwd":
        this.setData({
          rePwd: util.trim(e.detail.value)
        });
        break;
    }
  },
  sendSms: function () {
    var self = this
    var phone = self.data.phone;
    if (self.data.sending) return
    var json = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"phone":"' + phone + '"}}'
    self.data.sending = true
    wx.showLoading({
      title: '请稍等...',
      mask:true
    })
    clearInterval(self.data.time)
    api.get_M_Sms({
      method: 'POST',
      data: { REQ_MESSAGE: json },
      complete: (res) => {
        wx.hideLoading()
        if (res.statusCode == 200) {
          if (res.data.REP_BODY.RSPCOD == "000000") {
            self.data.time = setInterval(function () {
              var text = ""
              if (self.data.duration > 0) {
                text = "重新发送(" + self.data.duration-- + ")"
              } else {
                text = "获取验证码"
                self.data.duration = 120;
                self.data.sending = false;
                clearInterval(self.data.time)
              }
              self.setData({
                v_text: text,
                duration: self.data.duration
              })
            }, 1000)
          } else {
            self.showModal(res.data.REP_BODY.RSPMSG)
            self.data.sending = false
          }
        } else {
          self.showModal("获取短信验证码失败")
          self.data.sending = false
        }
      },
      fail: (res) => {
        self.data.sending = false
        wx.hideLoading()
        self.showModal("获取短信验证码失败")
        console.log("fail", res);
      }
    })
  },
  checkData: function (step) {
    var self = this
    if (step == "0") {
      if (self.data.userInfo == "") {
        if (util.trim(self.data.phone) == "" || !self.data.phone) {
          self.showModal("请输入手机号码")
          return false;
        }
        if (!util.check.phone(self.data.phone)) {
          self.showModal("请输入正确的手机号码")
          return false;
        }
      }
      if (util.trim(self.data.sms) == "" || !self.data.sms) {
        self.showModal("请输入短信验证码")
        return false;
      }
    } else if (step == "1") {
      if (util.trim(self.data.pwd) == "" || !self.data.pwd) {
        self.showModal("请输入密码")
        return false;
      }
      if (self.data.pwd < 6) {
        self.showModal("密码不能小于6位")
        return false;
      }
      if (util.trim(self.data.rePwd) == "" || !self.data.rePwd) {
        self.showModal("请输入重复密码密码")
        return false;
      }
      if (self.data.pwd != self.data.rePwd) {
        self.showModal("两次密码不一致")
        return false;
      }
    }
    return true
  },
  showModal: function (text) {
    wx.showModal({
      title: '提示',
      content: text,
      showCancel: false,
      confirmColor: "#e30c26",
      success: function (res) {
      }
    })
  },
  done: function (e) {
    var self = this
    var phone = self.data.phone;
    var jsonStr = '{}'
    if (!self.checkData(e.target.dataset.step)) return
    wx.showLoading({
      mask:true
    })
    if (e.target.dataset.step == 0) {
      jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"phone":"' + phone + '","code":"' + self.data.sms + '"}}'

      api.setSms({
        method: 'POST',
        data: { REQ_MESSAGE: jsonStr },
        complete: (res) => {
          wx.hideLoading()
          console.log(res);
          if (res.statusCode == 200) {
            if (res.data.REP_BODY.RSPCOD == "000000") {
              self.setData({
                step: 1
              })
            } else {
              self.showModal(res.data.REP_BODY.RSPMSG)
            }
          } else {
            self.showModal("短信验证失败")
          }
        },
        fail: (res) => {
          wx.hideLoading()
          console.log("fail", res);
        }
      })
    } else if (e.target.dataset.step == "1") {
      jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"phone":"' + phone + '","pwd":"' + self.data.pwd + '"}}'
      if (self.data.loginType || self.data.userInfo.LOGINTYPE){
        api.modifyPassword2({
          method: 'POST',
          data: { REQ_MESSAGE: jsonStr },
          success: (res) => {
            wx.hideLoading()
            if (res.REP_BODY.RSPCOD == "000000") {
              wx.showModal({
                title: '提示',
                content: '密码修改成功！请重新登录！',
                showCancel: false,
                confirmColor: "#e30c26",
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorage();
                    wx.switchTab({
                      url: '/pages/myPan/index/index'
                    })
                  }
                }
              })
            } else {
              self.showModal("密码修改失败!")
            }
          },
          fail: (res) => {
            console.log(res)
            wx.hideLoading()
          }
        })
      }else{
        api.modifyPassword({
          method: 'POST',
          data: { REQ_MESSAGE: jsonStr },
          success: (res) => {
            wx.hideLoading()
            if (res.REP_BODY.RSPCOD == "000000") {
              wx.showModal({
                title: '提示',
                content: '密码修改成功！请重新登录！',
                showCancel: false,
                confirmColor: "#e30c26",
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorage();
                    wx.switchTab({
                      url: '/pages/myPan/index/index'
                    })
                  }
                }
              })
            } else {
              self.showModal("密码修改失败!")
            }
          },
          fail: (res) => {
            console.log(res)
            wx.hideLoading()
          }
        })
      }
      
    }

  }, 
  checkedFn: function () {
    var that = this
    that.setData({
      loginType: that.data.loginType ? false : true
    });
  }
})